package cvi.com.reader.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.heima.tabview.library.TabView;
import com.heima.tabview.library.TabViewChild;
import com.orhanobut.hawk.Hawk;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cvi.com.reader.R;
import cvi.com.reader.base.BaseActivity;
import cvi.com.reader.bean.CategoriesBean;
import cvi.com.reader.bean.constant;
import cvi.com.reader.fragment.HomePageFragment;
import cvi.com.reader.fragment.LiteraryMasterFragment;
import cvi.com.reader.fragment.PersonalFragment;
import cvi.com.reader.fragment.PictureBookFragment;
import cvi.com.reader.fragment.PoetryCollectionFragment;
import cvi.com.reader.fragment.RandomReadingFragment;
import cvi.com.reader.fragment.TextBookFragment;
import cvi.com.reader.network.DefaultObserver;
import cvi.com.reader.network.NetWorkManager;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by WP on 2020/7/17.
 * 主界面
 */
public class MainActivity extends BaseActivity {
    @BindView(R.id.tabView)
    TabView tabView;
    private List<TabViewChild> tabViewChildList;
    private long exitTime = 0;
    private MyReceiver myReceiver;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        tabViewChildList = new ArrayList<>();
        TabViewChild tabViewChild03 = new TabViewChild(R.mipmap.sele_tab_home_page, R.mipmap.unsele_tab_home_page, "首页", new HomePageFragment());
        TabViewChild tabViewChild01 = new TabViewChild(R.mipmap.sele_tab_poetry_collection, R.mipmap.unsele_tab_poetry_collection, "诗词大汇", new PoetryCollectionFragment());
        TabViewChild tabViewChild02 = new TabViewChild(R.mipmap.sele_tab_text_book, R.mipmap.unsele_tab_text_book, "课本", new TextBookFragment());
        TabViewChild tabViewChild04 = new TabViewChild(R.mipmap.sele_tab_picture_book, R.mipmap.unsele_tab_picture_book, "示范诵读", new RandomReadingFragment());
        TabViewChild tabViewChild05 = new TabViewChild(R.mipmap.sele_tab_litrary_master, R.mipmap.unsele_tab_litrary_master, "文学巨匠", new LiteraryMasterFragment());
        tabViewChildList.add(tabViewChild03);
        tabViewChildList.add(tabViewChild01);
        tabViewChildList.add(tabViewChild02);
        tabViewChildList.add(tabViewChild04);
        tabViewChildList.add(tabViewChild05);
        //end add data
        tabView.setOnTabChildClickListener(new TabView.OnTabChildClickListener() {
            @Override
            public void onTabChildClick(int position, ImageView currentImageIcon, TextView currentTextView) {
                // Toast.makeText(getApplicationContext(),"position:"+position,Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (myReceiver != null) {
            unregisterReceiver(myReceiver);
        }
    }

    @Override
    protected void initData() {
        NetWorkManager.getInstance().getNetWorkApiService().getCategoriesData().compose(this.<JSONObject>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver<JSONObject>(MainActivity.this) {
                    @Override
                    protected void onSuccess(JSONObject response) {
                        //保存素材分类数据
                        if (response != null) {
                            savaData(response);
                        }
                    }
                });
    }

    private void savaData(JSONObject response) {
        JSONArray array = response.getJSONArray("rows");
        List<CategoriesBean.RowsBean> categoriesBeanList = new ArrayList<CategoriesBean.RowsBean>();
        for (int i = 0; i < array.size(); i++) {
            JSONObject object = (JSONObject) array.get(i);     //将array中的数据进行逐条转换
            CategoriesBean.RowsBean rowsBean = (CategoriesBean.RowsBean) JSONObject.toJavaObject(object, CategoriesBean.RowsBean.class);  //通过JSONObject.toBean()方法进行对象间的转换
            categoriesBeanList.add(rowsBean);
        }
        //保存至Hawk数据库
        Hawk.put("categoriesBeanList", categoriesBeanList);
        tabView.setTabViewDefaultPosition(0);
        tabView.setTabViewChild(tabViewChildList, getSupportFragmentManager());
    }


    @Override
    protected void extrasDoSome(Bundle extras) {

    }

    @Override
    public void onBackPressed() {
        exit();
    }

    public void exit() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            Toast.makeText(getApplicationContext(), "再按一次退出程序",
                    Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {
            finish();
            System.exit(0);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        myReceiver = new MyReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(constant.CHANGE_FRGAMENT);
        registerReceiver(myReceiver, intentFilter);
    }

    class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (Hawk.contains("CategoriesBean")) {
                CategoriesBean.RowsBean rowsBean = Hawk.get("CategoriesBean");
                if (rowsBean == null) {

                    return;
                }

                if (rowsBean.getName().equals("小学语文") || rowsBean.getName().equals("小学英语")) {


                } else if (rowsBean.getName().equals("唐诗") || rowsBean.getName().equals("宋词")) {

                }
            }

        }
    }
}
